package com.fhz.day007

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.fhz.day007.databinding.RvLayoutBinding

/**
 * 时间:2021/10/21
 * @author msfeng
 * 简述: TODO
 */
class MyAdapter(var list:List<User>): RecyclerView.Adapter<MyAdapter.MyViewHolder>() {


    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var bing: RvLayoutBinding? = DataBindingUtil.bind<RvLayoutBinding>(itemView)

        fun bind(user: User) {
            bing?.u = user
        }
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val user = list.get(position)
        holder.bind(user)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.rv_layout, null, false)
        return MyViewHolder(inflate)
    }


    companion object{
        @BindingAdapter("android:src")
        @JvmStatic
        fun setImage(img:ImageView,id:Int){
            img.setImageResource(id)
        }


        @BindingAdapter("url")
        @JvmStatic
        fun setUrl(img:ImageView,url:String){
            Glide.with(App.context).load(url).into(img)
        }
    }



}

